/* This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <avr/eeprom.h> 
#include <string.h>
#include <ctype.h>
#include "eeprom_handler.h"

#define DEFAULT_PHN_NO          "+46999999999"
#define DEFAULT_TEMP_WARN_LVL (14)

void eepInitEep(struct s_eepData* pEepData)
{
    static const int8_t defaultRelayValue = EEP_RELAY_OPEN;
    static const int8_t defaultTempWarnLvl = DEFAULT_TEMP_WARN_LVL;
    static const char defaultMasterPhnNo[] = DEFAULT_PHN_NO;

    eepSaveRelay1Value(defaultRelayValue);
    eepSaveRelay2Value(defaultRelayValue);
    eepSaveTempWarningLvl(defaultTempWarnLvl);
    eepSaveMasterPhoneNo((char*) defaultMasterPhnNo);

    pEepData->relay1Value = defaultRelayValue;
    pEepData->relay2Value = defaultRelayValue;
    pEepData->tempWarningLevel = defaultTempWarnLvl;
    memcpy(pEepData->masterPhoneNo, defaultMasterPhnNo, 
           strlen(defaultMasterPhnNo));
}

int8_t eepGetStoredData(struct s_eepData* pEepData)
{
    uint8_t temp, i;
    char digit;
    int8_t retVal = EEP_OK;

    temp = eeprom_read_byte((uint8_t*) EEP_RELAY1_VAL_ADDR);
    pEepData->relay1Value = * ((int8_t*) (&temp));
    if ((pEepData->relay1Value != EEP_RELAY_OPEN) && 
        (pEepData->relay1Value != EEP_RELAY_CLOSED)){
        retVal = EEP_NOK;
    }

    temp = eeprom_read_byte((uint8_t*) EEP_RELAY2_VAL_ADDR);
    pEepData->relay2Value = * ((int8_t*) (&temp));
    if ((pEepData->relay2Value != EEP_RELAY_OPEN) && 
        (pEepData->relay2Value != EEP_RELAY_CLOSED)){
        retVal = EEP_NOK;
    }
    
    temp = eeprom_read_byte((uint8_t*) EEP_TEMP_WARN_VAL_ADDR);
    pEepData->tempWarningLevel = * ((int8_t*) (&temp));
    if ((pEepData->tempWarningLevel < -10) || (pEepData->tempWarningLevel > 40)){
        retVal = EEP_NOK;
    }

    digit = 'A'; //Just something that is not 'E'
    for (i=0; i<15; i++){
        temp = eeprom_read_byte((uint8_t*) (EEP_PHONE_NO_START_ADDR+i));
        digit = * ((char*) (&temp));
        if(isdigit(digit) || (digit == '+')){
            pEepData->masterPhoneNo[i] = digit;
        } else if (digit == 'E') {
            //Found end character -> whole number has been fetched.
            break;
        } else {
            retVal = EEP_NOK;
            break;
        }
    }

    return retVal;
}

void eepSaveRelay1Value(int8_t value)
{
    uint8_t temp;
    temp = *((uint8_t*)(&value)); //Convert to unsigned

    eeprom_write_byte((uint8_t*) EEP_RELAY1_VAL_ADDR, temp);
}

void eepSaveRelay2Value(int8_t value)
{
    uint8_t temp;
    temp = *(uint8_t*)(&value); //Convert to unsigned

    eeprom_write_byte((uint8_t*) EEP_RELAY2_VAL_ADDR, temp);
}

void eepSaveTempWarningLvl(int8_t level)
{
    uint8_t temp;
    temp = *(uint8_t*)(&level); //Convert to unsigned

    eeprom_write_byte((uint8_t*) EEP_TEMP_WARN_VAL_ADDR, temp);
}

void eepSaveMasterPhoneNo(char* phnNo)
{
    uint8_t i;

    for(i=0; i< strlen(phnNo); i++){
      eeprom_write_byte((uint8_t*) (EEP_PHONE_NO_START_ADDR + i), phnNo[i]);
    }
    eeprom_write_byte((uint8_t*) (EEP_PHONE_NO_START_ADDR + i), 
                      EEPROM_PHONE_NO_END_CHAR);        
}
